<?php
/**
 * Desc:
 * User: hollowcg@foxmail.com
 * Date: 2019/5/25
 */

namespace app\common\model;


use think\Db;
use think\Model;
use think\Request;

class UserRole extends Model
{
    /**
     * 获取数据列表
     * @param $data
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getDataArr($data){
        isset($data['page'])?$page = $data['page']:$page = 1;
        isset($data['limit'])?$limit = $data['limit']:$limit = 10;

        $data = Db::name('UserRole')->select();
        $count = Db::name('UserRole')->count();
        return apiResult($data,0,'',$count);
    }

    /**
     * 保存
     * @param $data
     * @param $id
     * @return array
     */
    public static function saveData($data,$id){

        try{

            $validate = [
                'name|名称'=>"unique:userRole"
            ];

            if (empty($id)){
                $result = \model('userRole')->validate($validate)->allowField(true)->isUpdate(false)->save($data);
            }else{
                $result = \model('userRole')->validate($validate)->allowField(true)->isUpdate(true)->save($data,['id'=>$id]);
            }

            if(false === $result){
                // 验证失败 输出错误信息
                return apiResult('',1,\model('userRole')->getError());
            }

            return apiResult(\model('userRole')->getLastInsID(),0,'操作成功');

        }catch (\Exception $e){
            return apiResult('',1,$e->getMessage());
        }

    }
}